#include "XWindowImpl.h"
#include "core/Core.h"
#include "gui/iGraphicsSubsystem.h"
#include "XGraphicsSubsystem.h"

#include <X11/Xlib.h>

#include <string>
using std::string;

using namespace neiderra::gui;
using namespace neiderra::rendering;
using namespace neiderra::core;

XWindowImpl::XWindowImpl(Core* core, const string& title, GUIWindow* container): GUIWindowImpl( core , container)
{
	_display = ((XGraphicsSubsystem*)core->getGraphicsSubsystem())->GetDisplay();
	_title = title;
	
	int blackColor = BlackPixel(_display, DefaultScreen(_display));
	int whiteColor = WhitePixel(_display, DefaultScreen(_display));
	
	_gc = XCreateGC(_display, _window, 0, 0);
	
	_window = XCreateSimpleWindow(_display, DefaultRootWindow(_display), 0, 0, 
											 640, 480, 0, blackColor, blackColor);
	
	XSelectInput(_display, _window, StructureNotifyMask);
}

XWindowImpl::~ XWindowImpl()
{
	XFreeGC(_display, _gc);
	// free _gc
	// free _window
}

int XWindowImpl::ResponseEvent( XEvent& e )
{
	switch(e.type)
	{
		case MapNotify:
			
			this->show();
			this->swapBuffers();
			
			break;
	}	
	return 0;
}

unsigned XWindowImpl::getHeight()
{
	return 600;
}

unsigned XWindowImpl::getWidth()
{
	return 800;
}

void XWindowImpl::swapBuffers()
{
	XFlush(_display);
}

void XWindowImpl::setRenderer( Renderer* r )
{
	_renderer = r;
}

void XWindowImpl::show()
{
	XMapWindow(_display, _window);
}

void XWindowImpl::update()
{
}
